home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / handson / Java / SCAdventure / Adventure.java < prev    next >
Encoding:
Java Source  |  1998-04-27  |  5.8 KB  |  150 lines

  1. import java.lang.*;
  2. import java.io.*;
  3. import java.util.*;
  4. // This is the 'main' Adventure unit - the object that contains the map of Rooms
  5. // and their contents
  6.  
  7. public class Adventure extends java.lang.Object 
  8.                        implements java.io.Serializable,
  9.                        AdventureConstants 
  10.                        {
  11.  
  12.     private Vector map = new Vector(); // the map - a Vector of Rooms 
  13.     private AdvUtils Utils = new AdvUtils(); // My handy toolkit
  14.     private Person player;             // the player - which provides our 'first person perspective'
  15.     
  16.     /******** THE MAP *******
  17.     For reference, here is a simple 'picture' of the map 
  18.     showing how the rooms are connected:
  19.     
  20.     room0 -- room1
  21.       |
  22.     room2 -- room3
  23.       |        |
  24.     room4 -- room5
  25.        
  26.     *************************/
  27.     
  28.     Adventure() {
  29.         // --- construct a new adventure ---
  30.         // Add Rooms to the map
  31.         //                 Room( name,   description,                                                 N,        S,      W,      E )
  32.         map.addElement(new Room("room0, West of House", "An open field west of a white house",       NOEXIT,   2,      NOEXIT, 1));
  33.         map.addElement(new Room("room1, White House",   "A white house in the Mexican style",        NOEXIT,   NOEXIT, 0,      NOEXIT));
  34.         map.addElement(new Room("room2, Gold room",     "A golden room hung with rich tapestries",   0,        4,      NOEXIT, 3));
  35.         map.addElement(new Room("room3, Troll room",    "A dank, evil-smelling dungeon",             NOEXIT,   5,      2,      NOEXIT));
  36.         map.addElement(new Room("room4, Dark cave",     "It's too dark to see anything!",            2,        NOEXIT, NOEXIT, 5));
  37.         map.addElement(new Room("room5, Orchard",       "A sweetly-scented orchard",                 3,        NOEXIT, 4,      NOEXIT));
  38.         // --- Add things to selected Rooms
  39.         // room0
  40.         ((Room)map.elementAt(0)).addthing(new Thing("Sword", "An Elvish sword of great antiquity"));
  41.         ((Room)map.elementAt(0)).addthing(new Thing("Bubblegum", "A pink, sticky, dried-up blob"));
  42.         ((Room)map.elementAt(0)).addthing(new Thing("Dagger", "A blood-stained dagger"));
  43.         // room3
  44.         ((Room)map.elementAt(3)).addthing(new Thing("Chewing gum", "A grey, sticky, dried-up blob"));
  45.         // room5
  46.         ((Room)map.elementAt(5)).addthing(new Thing("Fluff", "Some pocket fluff"));
  47.         ((Room)map.elementAt(5)).addthing(new Thing("Coin", "A silver sixpenny bit"));
  48.         ((Room)map.elementAt(5)).addthing(new Thing("Pot of noodles", "A vaguely inedible-looking delicacy"));
  49.         // create player and place in Room 0 (i.e. the Room at 0 index of map Vector)
  50.         player = new Person((Room)map.elementAt(0));
  51.     }
  52.     
  53.     // access methods
  54.     // map
  55.     Vector getmap() {
  56.         return map;
  57.     }
  58.     
  59.     void setmap(Vector aMap) {
  60.         map = aMap;
  61.     }
  62.     
  63.     // player
  64.     Person getplayer() {
  65.         return player;
  66.     }
  67.     
  68.     void setplayer(Person aPlayer) {
  69.         player = aPlayer;
  70.     }
  71.     
  72.     // move a Person (typically the player) to a Room
  73.     void movePersonTo( Person p, Room aRoom ) {
  74.         p.setroom( aRoom );
  75.     }
  76.     
  77.     // move a Person 'p' in direction 'dir'
  78.     int moveTo( Person p, int dir ) {
  79.         // return: Constant representing the room number moved to
  80.         // or NOEXIT
  81.         //
  82.         // try to move any Person (typically but not necessarily player)
  83.         // in direction indicated by dir
  84.         Room r = p.getroom();
  85.         int exit;
  86.        
  87.         switch( dir ) {
  88.             case NORTH : exit = r.getn();
  89.                     break;
  90.             case SOUTH : exit = r.gets();
  91.                     break;
  92.             case EAST : exit = r.gete();
  93.                     break;
  94.             case WEST : exit = r.getw();
  95.                     break;  
  96.             default: exit = NOEXIT; // noexit - stay in same room
  97.                     break;
  98.         }
  99.        if (exit > NOEXIT)
  100.             movePersonTo( p,(Room) map.elementAt(exit) ); 
  101.        return exit;
  102.     }
  103.     
  104.     String movePlayerTo(int dir) {
  105.         // !!! Now returns a String (a message to be displayed) rather than an integer
  106.         // return: Constant representing the direction moved
  107.         // or NOEXIT (see moveTo())
  108.         //        
  109.         if (moveTo(player, dir ) == NOEXIT)
  110.            return "No Exit!";
  111.         else return ""; 
  112.     }
  113.  
  114.  
  115.  
  116.     String takeOb( String obname ) {
  117.         // If object specified by obname is in current room, Transfer it from
  118.         // Room's object Vector to Player's object Vector
  119.         Vector inventory = player.getthings();
  120.         Vector thingshere = player.getroom().getthings();
  121.         int obindex = Utils.ObNameAtIndex( obname, thingshere );
  122.         if (obname.equals(""))
  123.            return ("I don't know which thing you want to take!");
  124.         else if (obindex == -1) 
  125.             return "There is no " + obname + " here!";
  126.         else {
  127.             Utils.TransferOb(thingshere.elementAt(obindex), thingshere, inventory );
  128.             return obname + " taken!";
  129.         }
  130.     }
  131.     
  132.     
  133.     String dropOb( String obname ) {
  134.         // If object specified by obname is in inventory, Transfer it from
  135.         // Player's object Vector to Room's object Vector
  136.         Vector inventory = player.getthings();
  137.         Vector thingshere = player.getroom().getthings();        
  138.         int obindex = Utils.ObNameAtIndex( obname, inventory );
  139.         if (obname.equals(""))
  140.            return ("I don't know which thing you want to drop!");        
  141.         if (obindex == -1) 
  142.             return "You haven't got the " + obname + "!";
  143.         else {
  144.             Utils.TransferOb(inventory.elementAt(obindex), inventory, thingshere );
  145.             return obname + " dropped!";
  146.         }
  147.     }
  148.     
  149. }
  150.